package com.ruoyi.tj.utils;

import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field;

@Slf4j
public class ObjectUtils {

    public static void setEmptyStringFieldsNull(Object obj) {
        if(obj == null || obj.getClass().equals(Object.class)) {
            return;
        }
        Class<?> tmpCls = obj.getClass();
        while(!tmpCls.equals(Object.class)) {
            try {
                Field[] fields = tmpCls.getDeclaredFields();
                for (Field field : fields) {
                    field.setAccessible(true);
                    if(field.getType().equals(String.class)) {
                        Object val = field.get(obj);
                        if(val == null || "".equals(val.toString())) {
                            field.set(obj, null);
                        }
                    }

                }
            } catch (Throwable throwable) {
                log.error("setEmptyFieldsNull异常", throwable);
            }
            tmpCls = tmpCls.getSuperclass();
        }
    }

}
